package cn.stylefeng.guns.modular.lx.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.lx.entity.LxDemo;
import cn.stylefeng.guns.modular.lx.mapper.LxDemoMapper;
import cn.stylefeng.guns.modular.lx.model.params.LxDemoParam;
import cn.stylefeng.guns.modular.lx.model.result.LxDemoResult;
import  cn.stylefeng.guns.modular.lx.service.LxDemoService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 练习示例表 服务实现类
 * </p>
 *
 * @author zhucx
 * @since 2023-12-01
 */
@Service
public class LxDemoServiceImpl extends ServiceImpl<LxDemoMapper, LxDemo> implements LxDemoService {

    @Override
    public void add(LxDemoParam param){
        LxDemo entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(LxDemoParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(LxDemoParam param){
        LxDemo oldEntity = getOldEntity(param);
        LxDemo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public LxDemoResult findBySpec(LxDemoParam param){
        return null;
    }

    @Override
    public List<LxDemoResult> findListBySpec(LxDemoParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(LxDemoParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(LxDemoParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private LxDemo getOldEntity(LxDemoParam param) {
        return this.getById(getKey(param));
    }

    private LxDemo getEntity(LxDemoParam param) {
        LxDemo entity = new LxDemo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
